#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QXmlStreamWriter>
#include <QMessageBox>
#include <QFile>
//#include <QTextStream>
#include <QTextCodec>
#include <QDateEdit>
#include <iostream>

#include <SingleTaxReport.h>

#include <QIntValidator>
#include <QRegExpValidator>

#include <PersonalInfoDialog.h>
#include <QSettings>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{


    ui->setupUi(this);

    PopulateYearCombo();

    InsertTablesAndButton();

    SetValidators();

    LoadSettings();


}

void MainWindow::PopulateYearCombo()
{
    QString currentYear = QString::number(QDate::currentDate().year());
    QString previousYear = QString::number(QDate::currentDate().year() - 1);

    ui->yearCombo->addItem(previousYear);
    ui->yearCombo->addItem(currentYear);

    ui->yearCombo->setCurrentIndex(ui->yearCombo->count() - 1);
}

void MainWindow::InsertTablesAndButton()
{
    ui->formLayout->insertRow(4, ui->activityTable);

    QHBoxLayout * activityLayout = new QHBoxLayout(ui->formLayoutWidget);

    activityLayout->addWidget(ui->addActivityButton);
    activityLayout->addWidget(ui->removeActivityButton);

    ui->formLayout->insertRow(5, activityLayout);
    //ui->formLayout->insertRow(5, ui->addActivityButton);

    ui->formLayout->insertRow(6, ui->paymentTable);

    QHBoxLayout * paymentLayout = new QHBoxLayout(ui->formLayoutWidget);

    paymentLayout->addWidget(ui->addPaymentButton);
    paymentLayout->addWidget(ui->removePaymentButton);

    ui->formLayout->insertRow(7, paymentLayout);
    //ui->formLayout->insertRow(7, ui->addPaymentButton);

    ui->formLayout->addRow(ui->createReportButton);
}

void MainWindow::SetValidators()
{

    // TODO: figure out the regexp or other way to validate the name!
    QRegExp taxPayerNameRegExp = QRegExp("[[:alpha:]]", Qt::CaseSensitive, QRegExp::WildcardUnix);
    QRegExpValidator * taxPayerNameValidator = new QRegExpValidator(taxPayerNameRegExp, this);
    //ui->taxPayerNameEdit->setValidator(taxPayerNameValidator);

    QIntValidator * taxOfficeNameValidator = new QIntValidator(0, 9999, this);
    ui->taxOfficeNumberEdit->setValidator(taxOfficeNameValidator);

    QDoubleValidator * taxPayerIdValidator = new QDoubleValidator(0, 99999999999, 0, this);

    ui->taxPayerIdEdit->setValidator(taxPayerIdValidator);

    QIntValidator * taxPaidValidator = new QIntValidator(this);
    taxPaidValidator->setBottom(0);
    ui->taxPaidEdit->setValidator(taxPaidValidator);

    QIntValidator * moneyEarnedValidator = new QIntValidator(0, 5000000, this);
    ui->moneyEarnedEdit->setValidator(moneyEarnedValidator);
}

void MainWindow::LoadSettings()
{
    QSettings settings("oksamyt", "taxreport");

    ui->taxOfficeNumberEdit->setText(settings.value("taxOfficeName").toString());
    ui->taxPayerIdEdit->setText(settings.value("taxPayerId").toString());
    ui->taxPayerNameEdit->setText(settings.value("taxPayerName").toString());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void MainWindow::on_addActivityButton_clicked()
{
    ui->activityTable->insertRow(ui->activityTable->rowCount());
}

void MainWindow::on_addPaymentButton_clicked()
{
    ui->paymentTable->insertRow(ui->paymentTable->rowCount());
    QDateEdit * dateEdit = new QDateEdit(QDate::currentDate());

    ui->paymentTable->setCellWidget(ui->paymentTable->rowCount() - 1, 0, dateEdit);

}

void MainWindow::on_createReportButton_clicked()
{
    SingleTaxReport rep;

    rep.SetTaxPayerId(ui->taxPayerIdEdit->text().toULong());
    rep.SetTaxPayerName(ui->taxPayerNameEdit->text());
    //rep.SetReportPeriod(ui->quarterCombo->currentText().toInt());
    rep.SetReportQuarter(ui->quarterCombo->currentText().toInt());
    rep.SetReportYear(ui->yearCombo->currentText().toInt());

    std::vector<Activity> activityVector;

    for (unsigned short i = 0; i < ui->activityTable->rowCount(); i++)
    {
        QTableWidgetItem* item = ui->activityTable->item(i, 0);

        Activity activity;

        if (item)
        {
            activity.code = item->text();
        }

        item = ui->activityTable->item(i, 1);

        if (item)
        {
            activity.name = item->text();
        }

        activityVector.push_back(activity);
    }

    rep.SetActivities(activityVector);

    std::vector<Payment> paymentVector;

    for (unsigned short i = 0; i < ui->paymentTable->rowCount(); i++)
    {
        QTableWidgetItem* item = ui->paymentTable->item(i, 0);

        Payment payment;

        if (item)
        {
            //payment.paymentdate = item->text();
        }

        QWidget * cellWidget = ui->paymentTable->cellWidget(i, 0);

        QDateEdit * dateEdit = (QDateEdit*)cellWidget;

        if (dateEdit)
        {
            payment.paymentDate = Date(dateEdit->date().year(), dateEdit->date().month(), dateEdit->date().day());
        }

        item = ui->paymentTable->item(i, 1);

        if (item)
        {
            payment.type = item->text();
        }

        item = ui->paymentTable->item(i, 2);

        if (item)
        {
            payment.id = item->text();
        }

        paymentVector.push_back(payment);
    }

    rep.SetPayments(paymentVector);


    rep.SetTaxPaid(ui->taxPaidEdit->text().toInt());
    rep.SetEarnedSum(ui->moneyEarnedEdit->text().toInt());

    rep.WriteToFile(rep.GenerateFileName());

    //QMessageBox::warning(NULL, "test", rep.GenerateFileName().c_str());
}

void MainWindow::on_removeActivityButton_clicked()
{
    ui->activityTable->removeRow(ui->activityTable->currentRow());
}

void MainWindow::on_removePaymentButton_clicked()
{
    ui->paymentTable->removeRow(ui->paymentTable->currentRow());
}

void MainWindow::on_actionExit_triggered()
{
    close();
}

void MainWindow::on_actionSave_personal_info_triggered()
{
    PersonalInfo personalInfo;
    personalInfo.taxPayerName = ui->taxPayerNameEdit->text();
    personalInfo.taxOfficeName = ui->taxOfficeNumberEdit->text();
    personalInfo.taxPayerId = ui->taxPayerIdEdit->text();

    for (unsigned short i = 0; i < ui->activityTable->rowCount(); i++)
    {
        QTableWidgetItem* item = ui->activityTable->item(i, 0);

        Activity activity;

        if (item)
        {
            activity.code = item->text();
        }

        item = ui->activityTable->item(i, 1);

        if (item)
        {
            activity.name = item->text();
        }

        personalInfo.activities.push_back(activity);
    }

    PersonalInfoDialog * personalInfoDialog = new PersonalInfoDialog(personalInfo, this);

    personalInfoDialog->show();

}
